from rest_framework import serializers


from question_app.models import Question




# 创建增加问题序列化器
class QuestionAddSerializer(serializers.ModelSerializer):
  title = serializers.CharField(max_length=200)
  content = serializers.CharField(max_length=1000,allow_blank=True,allow_null=True)
  section = serializers.IntegerField()


  class Meta:
    model = Question
    fields = ['title','content','section']


# 创建回答问题序列化器
class AnswerAddSerializer(serializers.ModelSerializer):
  content = serializers.CharField(max_length=1000,allow_blank=True,allow_null=True)
  question = serializers.IntegerField()
  section = serializers.IntegerField()

  class Meta:
    model = Question
    fields = ['id','content','question','section']
    
class AnswerSerializer(serializers.ModelSerializer):
  # 获取用户的昵称
  user_name = serializers.CharField(source='user.nickname')
  # 设置格式化
  create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
  # 设置回答问题的序列化器
  class Meta:
    model = Question
    fields = ['user_name','content','create_time']
# 获取单个问题序列化器
class QuestionDetailSerializer(serializers.ModelSerializer):
  # 获取用户的昵称
  user_name = serializers.CharField(source='user.nickname')
  # 获取问题所在哪个课程分类
  course_name = serializers.CharField(source='section.chapter.stage.course.name')
  # 获取问题所在哪个课程阶段
  stage_name = serializers.CharField(source='section.chapter.stage.name')
  # 获取问题所在哪个课程章节
  chapter_name = serializers.CharField(source='section.chapter.name')
  # 获取问题所在哪个课程小节
  section_name = serializers.CharField(source='section.name')
  # 设置格式化
  create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
  # 设置回答问题的序列化器
  answers = AnswerSerializer(many=True,read_only=True,source='children')
  class Meta:
    model = Question
    fields = '__all__'
    
    


